/**
 * 
 */
package ar.edu.unq.svnpermission.ui.pages;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import ar.edu.unq.svnpermission.ui.exceptions.UserAlreadyExist;
import ar.edu.unq.svnpermission.ui.model.NewUser;

/**
 * Funciona como controlador de la Creacion de un nuevo Usuario
 * @author nicolas
 */
public class NewUserPage extends InternalPage<NewUser> {

	private static final long serialVersionUID = 1L;
	public final static String FORM = "newUserForm";

	public NewUserPage()
	{
		super();
	}
	
	
	@Override
	protected void addFields() {
		//Agrego los componentes
		this.getForm().add(new TextField<String>("name"));
		this.getForm().add(new TextField<String>("email"));		
		this.getForm().add(new TextField<String>("lastName"));
		this.getForm().add(new PasswordTextField("password"));	
		this.getForm().add(new FeedbackPanel("errorPanel"));
	}

	@Override
	protected void addButtons() {
		
		//Agrego el boton de Creación
		this.getForm().add(new Button("createuser") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				try {
					//Implemento la logica de negocios del boton "Crear"
					((NewUser) this.getParent().getDefaultModelObject()).create();
					setResponsePage(new UsersPage());
				}
				catch (UserAlreadyExist e) {
					//Muestro en el panel de error el mensaje de la excepcion
					NewUserPage.this.getForm().get("errorPanel").error(e.getMessage());
				}
			}
		});		
		
	}

	@Override
	protected CompoundPropertyModel<NewUser> createModel() {
		return new CompoundPropertyModel<NewUser>(new NewUser());
	}

	@Override
	protected String getFormName() {
		return NewUserPage.FORM;
	}

}
